﻿
namespace HSS.Xaml.UI.Controls
{
	#region Using Directives
	using System.Windows;
	using System.Windows.Controls;
	using System.Windows.Input;
	#endregion

	#region PropertyGridValueControl
	/// <summary>
	/// 
	/// </summary>
	[TemplatePart(Name = PART_ValueIndicatorControl, Type = typeof(ContentControl))]
	[TemplatePart(Name = PART_ValueActionControl, Type = typeof(ContentControl))]
	public class PropertyGridValueControl : ContentControl
	{
		#region Parts
		/// <summary>
		/// Gets the ValueIndicatorControl Part
		/// </summary>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "PART")]
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
		public const string PART_ValueIndicatorControl = "ValueIndicatorControl";
		/// <summary>
		/// Gets the ValueActionControl Part
		/// </summary>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "PART")]
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
		public const string PART_ValueActionControl = "ValueActionControl";
		#endregion

		#region Fields
		private ContentControl valueIndicatorControl;
		private ContentControl valueActionControl;
		private PropertyGridEntry propertyEntry;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructor
		/// </summary>
		public PropertyGridValueControl()
		{
			base.DefaultStyleKey = typeof(PropertyGridValueControl);
		}
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the PropertyGrid associated with this Item.
		/// </summary>
		internal PropertyGrid PropertyGrid
		{
			get { return _propertyGrid; }
			set { _propertyGrid = value; }
		} PropertyGrid _propertyGrid;
		internal object CustomContent { get; set; }
		#endregion

		#region Overrides
		/// <summary>
		/// Apply the control Template.
		/// </summary>
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.IsTabStop = false;
			this.valueIndicatorControl = base.GetTemplateChild(PART_ValueIndicatorControl) as ContentControl;
			this.valueIndicatorControl.Content = this.CustomContent;
			if (null == this.CustomContent)
				this.valueIndicatorControl.Visibility = Visibility.Collapsed;
			this.valueActionControl = base.GetTemplateChild(PART_ValueActionControl) as ContentControl;
		}
		/// <summary>
		/// Raises the Mouse Left Button Down event.
		/// </summary>
		/// <param name="e">The <see cref="MouseButtonEventArgs"/>.</param>
		protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
		{
			base.OnMouseLeftButtonDown(e);
			if (!this.propertyEntry.Descriptor.IsReadOnly)
				this.propertyEntry.IsEditorFocused = true;
			this.propertyEntry.Activate();
			if (this.propertyEntry.Descriptor.IsReadOnly)
				this.propertyEntry.Editor.FocusDisplayControl();
		}
		#endregion

		#region Methods
		internal void Initialize(PropertyGridEntry item)
		{
			this.propertyEntry = item;
		}
		private void HidePreviousEditor()
		{
			var activeEditor = this.PropertyGrid.ActiveEditor;
			if (activeEditor != null)
				activeEditor.PropertyEntry.ValueControl.ShowDialogEditButton(false);
		}
		internal void ShowDialogEditButton(bool show)
		{
			var valueActionButton = this.propertyEntry.Editor.GetDialogEditButton();
			if (null == valueActionButton)
				return;
			if (show)
			{
				valueActionButton.Click += new RoutedEventHandler(this.ValueActionButton_Click);
				if (null != this.valueActionControl)
					this.valueActionControl.Content = valueActionButton;
			}
			else
			{
				valueActionButton.Click -= new RoutedEventHandler(this.ValueActionButton_Click);
				if (null != this.valueActionControl)
					this.valueActionControl.Content = null;
			}
		}
		#endregion

		#region Event Handlers
		private void ValueActionButton_Click(object sender, RoutedEventArgs e)
		{
			this.propertyEntry.Editor.ShowEditDialog();
		}
		#endregion
	}
	#endregion
}